using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace HareGame
{
    public enum AnimType
    {
        None,
        缩小,
        放大,
    }

    public class MyButton : Button
    {
        [Header("点击音效")]
        public string ClipName = QAssetBundle.Yx_button_mp3.YX_button;

        [Header("点击动画类型")]
        public AnimType AnimType = AnimType.None;
        private Vector3 _defaultScale;

        #region 长按
        [Header("是否为长按按钮")]
        public bool IsLongPrassButton = false;

        [SerializeField]
        private UnityEvent _onLongPrass = new UnityEvent();//长按事件
        public UnityEvent OnLongPrass => _onLongPrass;

        private float _interval = 0.3f;//间隔
        private bool _isClick = false;//是否点击了
        private float _clickTime = 0;//点击记时

        private void Update()
        {
            if (Input.GetMouseButtonUp(0))
            {
                _isClick = false;
            }

            if (_isClick)
            {
                if (Time.time - _clickTime >= _interval)
                {
                    _onLongPrass?.Invoke();
                    _clickTime = Time.time;
                    _interval -= 0.1f;
                    if (_interval <= 0.1f)
                    {
                        _interval = 0.05f;
                    }
                }
            }
        }
        #endregion

        protected override void OnEnable()
        {
            base.OnEnable();

            _defaultScale = transform.localScale;

            if (IsLongPrassButton)
                _onLongPrass.AddListener(OnClick);
            else
                onClick.AddListener(OnClick);
        }

        void OnClick()
        {
            GameEntry.Audio.PlaySound(ClipName);
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);

            if (IsLongPrassButton)
            {
                _isClick = true;
                _interval = 0.3f;
                _clickTime = Time.time;
                _onLongPrass?.Invoke();
            }

            switch (AnimType)
            {
                case AnimType.缩小:
                    transform.DOKill();
                    transform.DOScale(_defaultScale - _defaultScale * 0.1f, 0.1f);
                    break;
                case AnimType.放大:
                    transform.DOKill();
                    transform.DOScale(_defaultScale + _defaultScale * 0.1f, 0.1f);
                    break;
            }
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);

            switch (AnimType)
            {
                case AnimType.缩小:
                case AnimType.放大:
                    transform.DOKill();
                    transform.DOScale(_defaultScale, 0.1f);
                    break;
            }
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            base.OnPointerExit(eventData);
            _isClick = false;
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            _isClick = false;
            onClick.RemoveListener(OnClick);
        }
    }
}